package com.flex.core.mail;

import java.io.IOException;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.flex.core.ServiceException;
import com.flex.core.freeMarker.FreeMarkerEngine;

/**
 * 发送邮件类,fromAddr和fromStatement在system.properties中进行配置
 * 
 * @author radio
 * 
 */
public class SystemMailSender {

	private String fromAddr;

	private String fromStatement;

	private JavaMailSender mailSender;

	@Autowired
	private FreeMarkerEngine freeMarkerEngine;

	/**
	 * 发送邮件，邮件内容为字符串<br>
	 * sendMail("邮件标题","邮件内容","123@sina.com")<br>
	 * sendMail("邮件标题","邮件内容","123@sina.com","456@sina.com")<br>
	 * sendMail("邮件标题","邮件内容","123@sina.com","456@sina.com","789@sina.com")<br>
	 * 或<br>
	 * String[] toAddr = {"123@sina.com","456@sina.com","789@sina.com"}<br>
	 * sendMail("邮件标题","邮件内容", toAddr)<br>
	 * 
	 * 
	 * @param subject
	 *            邮件标题
	 * @param content
	 *            邮件内容
	 * @param toAddr
	 *            收件人地址
	 * 
	 */
	public void sendMail(String subject, String content, String... toAddr) {
		MimeMessage msg = mailSender.createMimeMessage();
		try {
			MimeMessageHelper messageHelper = new MimeMessageHelper(msg, true,
					"UTF-8");
			messageHelper.setTo(toAddr);
			messageHelper.setSubject(subject);
			messageHelper.setText(content, true);
			messageHelper.setFrom(fromAddr, fromStatement);
			mailSender.send(msg);
		} catch (MessagingException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			throw new ServiceException("mail.error.SourceNotFound");
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException("unknow.Error");
		}
	}

	/**
	 * 发送邮件,邮件内容为指定的模板文件<br>
	 * sendMailByTemplate("邮件标题",ctx,"abc.flt",map,"123@sina.com")<br>
	 * sendMailByTemplate("邮件标题",ctx,"abc.flt",map,"123@sina.com","456@sina.com","789@sina.com")<br>
	 * 或<br> 
	 * String[] toAddr = {"123@sina.com","456@sina.com","789@sina.com"}<br>
	 * sendMailByTemplate("邮件标题",ctx,"abc.flt",map, toAddr)<br>
	 * 
	 * @param subject
	 *            邮件标题
	 * @param ctx
	 *            ServletContext定位模板目录
	 * @param templateName
	 *            模板文件名
	 * @param data
	 *            模板所需数据
	 * @param toAddr
	 *            收件人地址
	 *            
	 * @see FreeMarkerEngine
	 * 
	 */
	public void sendMailByTemplate(String subject, ServletContext ctx,
			String templateName, Map<String, Object> data, String... toAddr) {
		String content = freeMarkerEngine.getHtml(data, ctx, templateName);
		sendMail(subject, content, toAddr);
	}

	public void setFromStatement(String fromStatement) {
		this.fromStatement = fromStatement;
	}

	public void setFromAddr(String fromAddr) {
		this.fromAddr = fromAddr;
	}

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

}
